/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package post.dataaccess;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import post.ProductSpec;

/**
 *
 * @author Harry/Ayush
 */
public class ProductReader {           
    
    ProductSpec[] products;
    static int position=0;
    static private Comparator<ProductSpec> name;
    
    static {
        name = new Comparator<ProductSpec>(){
            @Override
            public int compare(ProductSpec o1, ProductSpec o2){
                return o1.getDescription().compareTo(o2.getDescription());
            }
        };
    }
    
    public ProductReader(String productFile) throws IOException {
        products=new ProductSpec[100];
        try {
            
            FileReader fr = new FileReader(productFile);
            /*location of products.txt file will be passed as the arguement, pls change the location accordingly.*/
            
            BufferedReader br = new BufferedReader(fr);
            boolean flag = true;        
            int i=0;
            
            while(true)
                {
                    String s = br.readLine();   //Reads the file line by line
                    if(s==null)                 //comparison to determine end of file (null)
                    {
                        break;                    
                    }                    
                    String[] temp= new String[3];
                    temp=s.split(",");                      
                    products[i]=new ProductSpec(temp[0],temp[1],Float.parseFloat(temp[2]));                    
                    i++;
                }
        } catch (Exception ex) {
            System.out.println( ex.getMessage( ) );
            Logger.getLogger(ProductReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        
      
    }

    public boolean hasMoreProducts() {
        if (products.length==position)
            return false;
        else 
            return true;
    }

    public ProductSpec getNextProduct() {
        return products[position++];
        
    }

    public ProductSpec[] loadAll() {

        return products;
    }

    public void addProduct(ProductSpec productSpec,String productFile) throws FileNotFoundException, IOException {        
        String newProduct=new String();
        newProduct=productSpec.getUpc()+","+productSpec.getDescription()+","+Float.toString(productSpec.getPrice())+"\n";
        FileWriter fw = new FileWriter(productFile,true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(newProduct);
        bw.close();        
    }
    
    public float getPriceByUPC(String UPC)
    {
        float price=0;        
        for (int i=0;i<products.length;i++) {            
            if (((products[i].getUpc()).compareTo(UPC))==0) {                
                price=products[i].getPrice();
                break;
            }
        }        
        return price;
    }
    public void sort(){                
        Arrays.sort(products, name);
        System.out.println("in sort");        
    }
    
    
    public void printProductsSortedByName(){  
            //this.sort();
            System.out.println("UPC \t Name \t Price");
            for (int i=0;i<products.length;i++){
                System.out.println(products[i].getUpc()+" \t "+products[i].getDescription()+" \t "+products[i].getPrice());
            }
            
    }
}
